package com.tqz.dt.source;

import com.tqz.dt.attribute.GlobalTransactionAttribute;
import com.tqz.dt.parser.GlobalSpringTransactionAnnotationParser;
import com.tqz.dt.parser.GlobalTransactionAnnotationParser;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
 * <p>注解的angle事务属性源
 *
 * @author tianqingzhao
 * @since 2021/7/7 15:27
 */
public class AnnotationGlobalTransactionAttributeSource extends AbstractGlobalTransactionAttributeSource {

    private Set<GlobalTransactionAnnotationParser> annotationParsers = new HashSet<GlobalTransactionAnnotationParser>();

    public AnnotationGlobalTransactionAttributeSource() {
        // 为注解的AngleTransactionAttributeSource设置解析器
        this.annotationParsers.add(new GlobalSpringTransactionAnnotationParser());
    }

    /**
     * 获取注解属性
     *
     * @param element 注解的元素信息
     * @return
     */
    public GlobalTransactionAttribute determineTransactionAttribute(AnnotatedElement element) {
        // 如果获取到注解属性对象
        if (element.getAnnotations().length > 0) {
            // 通过注解解析器来解析事务注解
            for (GlobalTransactionAnnotationParser annotationParser : this.annotationParsers) {
                GlobalTransactionAttribute attr = annotationParser.parseTransactionAnnotation(element);
                if (attr != null) {
                    return attr;
                }
            }
        }
        return null;
    }

    @Override
    protected GlobalTransactionAttribute findTransactionAttribute(Method method) {
        return determineTransactionAttribute(method);
    }
}
